#include "GLBufferManager.hh"
#include "oglHeaders.hh"

GLBufferManager* GLBufferManager::_mSingleton  = NULL;


GLBufferManager* GLBufferManager::GetSingleton(void)
{
   if(!_mSingleton) {
      _mSingleton = new GLBufferManager();
   }

   return _mSingleton;
}

GLBufferManager::GLBufferManager()
{
   
}

GLBufferManager::~GLBufferManager()
{
   
}


unsigned int GLBufferManager::CreateGLBuffer(const char* buf_name, void* local_buf_addr, unsigned int buf_byte_size)
{
   GLuint bufferID;
   glGenBuffers(1, &bufferID);

   if(glIsBuffer(bufferID)) {
      glBindBuffer(GL_ARRAY_BUFFER, bufferID);
      glBufferData(GL_ARRAY_BUFFER, buf_byte_size, local_buf_addr, GL_STATIC_DRAW);
      }

   glBindBuffer(GL_ARRAY_BUFFER, 0);

   _mBufferMap.insert(std::pair<std::string, unsigned int>(std::string(buf_name), bufferID) );

   return bufferID;
}

unsigned int GLBufferManager::GetGLBuffer(const char* buf_name)
{
   std::map<std::string, unsigned int>::iterator itr = _mBufferMap.find(std::string(buf_name));
   if(itr!=_mBufferMap.end()) {
      return itr->second;
   }

   return -1;
}
